package voice

import (
	"encoding/json"
	"fmt"
)

const (
	StatusVoiceFile新建记录 = 10
	StatusVoiceFile发起识别 = 20
	StatusVoiceFile任务排队 = 30
	StatusVoiceFile任务运行 = 40
	StatusVoiceFile没有语音 = 50
)

type ReqVoiceFile struct {
	Appkey         string `json:"appkey"` //应用 appkey，必填，专有云为 default
	Token          string `json:"token"`  //应用 token，必填，专有云为 default
	FileLink       string `json:"file_link"`
	EnableCallback bool   `json:"enable_callback,omitempty"`
	CallbackUrl    string `json:"callback_url,omitempty"`
	AutoSplit      bool   `json:"auto_split,omitempty"`
	ValidTimes     []struct {
		BeginTime int `json:"begin_time"`
		EndTime   int `json:"end_time"`
		ChannelId int `json:"channel_id"`
	} `json:"valid_times,omitempty"`
	ClassVocabularyId struct {
		Person  string `json:"PERSON"`
		Address string `json:"ADDRESS"`
	} `json:"class_vocabulary_id,omitempty"`
	CustomizationId                string `json:"customization_id,omitempty"`
	VocabularyId                   string `json:"vocabulary_id,omitempty"`
	Version                        string `json:"version,omitempty"`
	EnableWords                    bool   `json:"enable_words,omitempty"`
	EnableSampleRateAdaptive       bool   `json:"enable_sample_rate_adaptive,omitempty"`
	EnableUnifyPost                bool   `json:"enable_unify_post,omitempty"`
	EnableInverseTextNormalization bool   `json:"enable_inverse_text_normalization,omitempty"`
	MaxEndSilence                  int    `json:"max_end_silence,omitempty"`
	MaxSingleSegmentTime           int    `json:"max_single_segment_time,omitempty"`
	AsrCustomProperties            string `json:"asr_custom_properties,omitempty"`
}

type ReqCallbackVoiceFile struct {
	Result struct {
		Sentences Results `json:"sentences"`
	} `json:"result"`
	TaskId         string `json:"task_id"`
	StatusCode     int    `json:"status_code"`
	StatusText     string `json:"status_text"`
	RequestTime    int    `json:"request_time"`
	SolveTime      int    `json:"solve_time"`
	BizDuration    int    `json:"biz_duration"`
	EnableCallback bool   `json:"enable_callback"`
}

type Header struct {
	Appkey        string `json:"appkey"`
	GwTaskId      string `json:"gw_task_id"`
	MessageId     string `json:"message_id"`
	Namespace     string `json:"namespace"`
	Status        int    `json:"status"`
	StatusMessage string `json:"status_message"`
	TaskId        string `json:"task_id"`
}

type RespVoiceFile struct {
	Header Header `json:"header"`
}

type Results []Result

type Result struct {
	SilenceDuration int     `json:"silence_duration"`
	BeginTime       int64   `json:"begin_time"`
	EndTime         int64   `json:"end_time"`
	ChannelId       int     `json:"channel_id"`
	EmotionValue    float64 `json:"emotion_value"`
	SpeechRate      int     `json:"speech_rate"`
	Text            string  `json:"text"`
}

type VoiceFileResult struct {
	Result Results `json:"result"`

	TaskId         string `json:"task_id"`
	StatusCode     int    `json:"status_code"`
	StatusText     string `json:"status_text"`
	RequestTime    int64  `json:"request_time"`
	SolveTime      int64  `json:"solve_time"`
	BizDuration    int    `json:"biz_duration"`
	EnableCallback bool   `json:"enable_callback"`
}

type RespCallbackVoiceFileResult VoiceFileResult

type ReqVoiceFileResult struct {
	Header `json:"header"`
}

type RespVoiceFileResult struct {
	Header `json:"header"`

	Payload struct {
		Sentences Results `json:"sentences"`
	} `json:"payload"`
}

// 序列化
func (m *RespVoiceFileResult) MarshalBinary() (data []byte, err error) {
	fmt.Println("MarshalBinary")
	return json.Marshal(m)
}

// 反序列化
func (m *RespVoiceFileResult) UnmarshalBinary(data []byte) error {
	fmt.Println("UnmarshalBinary")
	return json.Unmarshal(data, m)

}

// //////////////////////////////
type CodeFile struct {
	Code    int
	Msg     string
	Content string
	Other   string
}

var CodeFiles = []CodeFile{
	{21050000, "SUCCESS", "成功", "POST⽅式的识别请求接⼝调⽤成功，或者GET⽅式的识别结果查询接⼝调⽤成功"},
	{21050001, "QUEUEING", "录⾳⽂件识别任务排队中", "请稍后再发送GET⽅式的识别结果查询请求"},
	{21050002, "RUNNING", "录⾳⽂件识别任务运⾏中", "请稍后再发送GET⽅式的识别结果查询请求"},
	{21050003, "SUCCESS_WITH_NO_VALId_FRAGMENT", "识别结果查询接⼝调⽤成功，但是没有识别到语⾳", "检查录⾳⽂件是否有语⾳，或者语⾳时⻓太短状态码,状态描述,状态含义,解决⽅案"},
	{41050001, "USER_BIZDURATION_QUOTA_EXCEED", "⽤户单⽇时间超限", "如业务量较⼤，请联系商务洽谈,邮件地址:nls_support@service.aliyun.com"},
	{41050002, "FILE_DOWNLOAD_FAILED", "⽂件下载失败", "检查录⾳⽂件路径是否正确，或阿⾥云的服务器是否可以正常访问下载"},
	{41050003, "FILE_CHECK_FAILED", "⽂件格式错误", "检查录⾳⽂件是否是单轨/双轨的WAV格式、MP3格式"},
	{41050004, "FILE_TOO_LARGE", "⽂件过⼤", "检查录⾳⽂件⼤⼩是否超过128MB"},
	{41050005, "FILE_NORMALIZE_FAILED", "⽂件归⼀化失败", "检查录⾳⽂件是否有损坏，是否可以正常播放"},
	{41050006, "FILE_PARSE_FAILED", "⽂件解析失败", "检查录⾳⽂件是否有损坏，是否可以正常播放"},
	{41050007, "MKV_PARSE_FAILED", "MKV解析失败", "检查录⾳⽂件是否有损坏，是否可以正常播放"},
	{41050008, "UNSUPPORTED_SAMPLE_RATE", "采样率不⽀持", "检查录⾳⽂件采样率是否是8000、16000"},
	{41050009, "UNSUPPORTED_ASR_GROUP", "ASR分组不⽀持", "确认下ak和appkey是否⼀致"},
	{41050010, "FILE_TRANS_TASK_EXPIRED", "录⾳⽂件识别任务过期", "TaskId不存在，或者已过期"},
	{41050011, "REQUEST_INVALId_FILE_URL_VALUE", "请求file_link参数⾮法", "请确认file_link参数格式是否正确"},
	{41050012, "REQUEST_INVALId_CALLBACK_VALUE", "请求callback_url参数⾮法", "请确认callback_url参数格式是否正确，是否为空"},
	{41050013, "REQUEST_PARAMETER_INVALId", "请求参数⽆效", "确认下请求task值为有效JSON格式字符串"},
	{41050014, "REQUEST_EMPTY_APPKEY_VALUE", "请求参数app_key值为空", "请确认是否设置了app_key参数值"},
	{41050015, "REQUEST_APPKEY_UNREGISTERED", "请求参数app_key未注册请确认请求参数app_key值是否设置正确，或者是否与阿⾥云账号的AccessKey", "Id同⼀个账号"},
	{51050000, "INTERNAL_ERROR", "内部通⽤错误", "如果偶现可以忽略，重复出现请提交⼯单"},
	{51050001, "VAD_FAILED", "VAD失败", "如果偶现可以忽略，重复出现请提交⼯单"},
	{51050002, "RECOGNIZE_FAILED", "内部alisr识别失败", "如果偶现可以忽略，重复出现请提交⼯单"},
	{51050003, "RECOGNIZE_INTERRUPT", "内部alisr识别中断", "如果偶现可以忽略，重复出现请提交⼯单"},
	{51050004, "OFFER_INTERRUPT", "内部写⼊队列中断", "如果偶现可以忽略，重复出现请提交⼯单"},
	{51050005, "FILE_TRANS_TIMEOUT", "内部整体超时失败", "如果偶现可以忽略，重复出现请提交⼯单"},
	{51050006, "FRAGMENT_FAILED", "内部分断失败", "如果偶现可以忽略，重复出现请提交⼯单"},
}
